﻿using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Furion.FriendlyException;
using Microsoft.AspNetCore.Authorization;
using System.Collections.Generic;

namespace Admin.NET.Application;
/// <summary>
/// 案例内容服务
/// </summary>
[AllowAnonymous]
[ApiDescriptionSettings(ApplicationConst.GroupNameApi, Order = 100)]
public class car_case_detailsServiceApi : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<car_case_details> _rep;
    public car_case_detailsServiceApi(SqlSugarRepository<car_case_details> rep)
    {
        _rep = rep;
    }

    /// <summary>
    /// 分页查询案例内容
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<car_case_detailsOutput>> Page(car_case_detailsInput input)
    {
        var query= _rep.AsQueryable()
                    .WhereIF(input.caseId>0, u => u.caseId == input.caseId)
                    .WhereIF(!string.IsNullOrWhiteSpace(input.url), u => u.url.Contains(input.url.Trim()))
                    .WhereIF(!string.IsNullOrWhiteSpace(input.des), u => u.des.Contains(input.des.Trim()))
                    .WhereIF(input.type>0, u => u.type == input.type)

                    .Select<car_case_detailsOutput>()
;
        query = query.OrderBuilder(input);
        return await query.ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加案例内容
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(Addcar_case_detailsInput input)
    {
        var entity = input.Adapt<car_case_details>();
        await _rep.InsertAsync(entity);
    }

    /// <summary>
    /// 删除案例内容
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(Deletecar_case_detailsInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.FakeDeleteAsync(entity);   //假删除
    }

    /// <summary>
    /// 更新案例内容
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(Updatecar_case_detailsInput input)
    {
        var entity = input.Adapt<car_case_details>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取案例内容
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<car_case_details> Get([FromQuery] QueryByIdcar_case_detailsInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.Id);
    }

    /// <summary>
    /// 获取案例内容列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<car_case_detailsOutput>> List([FromQuery] car_case_detailsInput input)
    {
        return await _rep.AsQueryable().Select<car_case_detailsOutput>().ToListAsync();
    }





}

